//
//  SettingViewController.m
//  JunZeWristband
//
//  Created by Kendama on 2017/9/6.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "WebViewController.h"
#import "LanguageSettingViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property(nonatomic,strong) NSArray *dataSource;

@end

@implementation SettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

- (void)refreshBaseControlValue
{
    
    
}

#pragma mark - 点击事件
- (void)onLeftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 事件处理
- (void)searchDevice
{
    DTCDeviceModel *device = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
    NSData *sendData = [NSString convertHexStrToData:searchDeviceCode];
    [[DTCBlueToothSendData sharedInstance] sendDataByData:sendData andInfo:device];
}


- (void)settingRadiateAlarmWithSwitch:(BOOL)isOn
{
    NSString *code = [[NSString alloc] init];
    if (isOn)
    {
        code = openRadiateAlarmCode;
        SAVE_BOOL(YES, [MAC stringByAppendingString:RadiateAlarm]);
    }
    else
    {
        code = closeRadiateAlarmCode;
        SAVE_BOOL(NO, [MAC stringByAppendingString:RadiateAlarm]);
    }
    DTCDeviceModel *device = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
    NSData *sendData = [NSString convertHexStrToData:code];
    [[DTCBlueToothSendData sharedInstance] sendDataByData:sendData andInfo:device];
}

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //    return self.dataSource.count;
    return 1;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(161);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SettingTableViewCell *cell = [SettingTableViewCell dequeueReusableWithTableView:tableView];
    cell.nameLabel.text = self.dataSource[indexPath.row];
    if (indexPath.row == 0)
    {
        if (EXRACT_BOOL([MAC stringByAppendingString:MessageRemind]))
        {
            cell.switchButton.selected = YES;
        }
        else
        {
            cell.switchButton.selected = NO;
        }
    }
    else if (indexPath.row == 1)
    {
        if (EXRACT_BOOL([MAC stringByAppendingString:CallRemind]))
        {
            cell.switchButton.selected = YES;
        }
        else
        {
            cell.switchButton.selected = NO;
        }
    }
    else if (indexPath.row == 2)
    {
        if (EXRACT_BOOL([MAC stringByAppendingString:RadiateAlarm]))
        {
            cell.switchButton.selected = YES;
        }
        else
        {
            cell.switchButton.selected = NO;
        }
    }
    
    __weak typeof(self) weakSelf = self;
    cell.switchBlock = ^(BOOL isOn) {
        switch (indexPath.row) {
            case 0:
            {
                if (isOn)
                {
                    [self closeCallAndMessagePushWithCode:closeMessagePushCode];
                }
                else
                {
                    [self openCallAndMessagePush];
                }
                SAVE_BOOL(isOn, [MAC stringByAppendingString:MessageRemind]);
            }
                break;
            case 1:
            {
                if (isOn)
                {
                    [self closeCallAndMessagePushWithCode:closeCallPushCode];
                }
                else
                {
                    [self openCallAndMessagePush];
                }
                SAVE_BOOL(isOn, [MAC stringByAppendingString:CallRemind]);
                          }
                break;
            case 2:
            {
                DTCDeviceModel *model = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
                if ([model.mac isEqualToString:EXRACT_OBJECT(MAC)])
                {
                    [weakSelf settingRadiateAlarmWithSwitch:isOn];
                }
                else
                {
                    [SVProgressHUD showErrorWithStatus:MyNSLocalizedString(@"设备未连接")];
                    [weakSelf.tableView reloadData];
                }
            }
                break;
                
            default:
                break;
        }
    };
    
    if (indexPath.row > 2)
    {
        NSString * const cellIdentifier = @"CellIdentifier";
        UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell2) {
            cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        }
        cell2.textLabel.font = [UIFont systemFontOfSize:15];
        cell2.textLabel.textColor = [UIColor darkGrayColor];
        cell2.textLabel.text = self.dataSource[indexPath.row];
        cell2.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.row == 4)
        {
            cell2.detailTextLabel.text = MyNSLocalizedString(@"寻找");
        }
        return cell2;
    }

    
    return cell;
}

- (void)closeCallAndMessagePushWithCode:(NSString *)code
{
    DTCDeviceModel *device = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
    NSData *sendData = [NSString convertHexStrToData:code];
    [[DTCBlueToothSendData sharedInstance] sendDataByData:sendData andInfo:device];
}

- (void)openCallAndMessagePush
{
    DTCDeviceModel *device = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
    NSData *sendData = [NSString convertHexStrToData:openPushCode];
    [[DTCBlueToothSendData sharedInstance] sendDataByData:sendData andInfo:device];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    cell.textLabel.text = self.dataSource[indexPath.row];
//    cell.textLabel.textColor = [UIColor darkGrayColor];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 3:
        {
            LanguageSettingViewController *vc = [[LanguageSettingViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            DTCDeviceModel *model = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
            if ([model.mac isEqualToString:EXRACT_OBJECT(MAC)])
            {
                [self searchDevice];
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:MyNSLocalizedString(@"设备未连接")];
            }
        }
            break;
        case 5:
        {
            WebViewController *vc = [[WebViewController alloc] init];
            vc.webViewType = Instructions;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - 通知处理

#pragma mark - 初始化
- (NSArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = @[MyNSLocalizedString(@"短信提醒"),MyNSLocalizedString(@"电话提醒"),MyNSLocalizedString(@"辐射报警"),MyNSLocalizedString(@"语言"),MyNSLocalizedString(@"一键寻找"),MyNSLocalizedString(@"使用帮助")];
    }
    return _dataSource;
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            //            [self getNewsListWithPage:1];
        //        }];
        [superView addSubview:_tableView];
    }
    return _tableView;
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:MyNSLocalizedString(@"设置")];
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(64);
        make.left.right.equalTo(superView);
        make.bottom.equalTo(superView);
    }];
}

@end
